#!/usr/bin/env bash
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.

DEFAULT_CCI=/c/cci
DEFAULT_LKP=/c/lkp-tests

[[ $CCI_SRC ]] || CCI_SRC=$DEFAULT_CCI
[[ $LKP_SRC ]] || LKP_SRC=$DEFAULT_LKP

if [ ! $1 ]; then
  echo "Usage: $0 [scheduler ...]"
  exit 1
fi

DIR=$(realpath "$1")
if [ ! -d "$DIR" ]; then
  echo "Service dir $DIR not exists"
  exit 1
fi
service=${DIR##*/}

cmd=(
  docker run
  --rm
  -e LKP_SRC=$DEFAULT_LKP
  -e CRYSTAL_PATH="lib:/usr/share/crystal/app/lib:/usr/lib/crystal/shards:/usr/lib/crystal/core:${DEFAULT_LKP}/lib:${DEFAULT_CCI}/src"
  -u $UID
  -v $LKP_SRC:$DEFAULT_LKP
  -v ${CCI_SRC}/src:${DEFAULT_CCI}/src
  -v $DIR:${DEFAULT_CCI}/compile
  -w ${DEFAULT_CCI}/compile
  alpine:scheduler-dev
  sh -c "crystal build --error-trace ${DEFAULT_CCI}/src/${service}.cr"
)

"${cmd[@]}"
